﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Pixysoft.Framework.Noebe.Core
{
    class PagingCommand : IPagingCommand
    {
        string tableName;
        string pagingColumnName;
        int pageCapacity;
        int pageIndex;
        bool reverse;
        string condition;

        NoebeCacheFactory cache = null;
        INoebeCommand command = null;

        /// <summary>
        /// Noebe Command
        /// </summary>
        public PagingCommand(NoebeCacheFactory aspect, INoebeCommand command)
        {
            this.cache = aspect;
            this.command = command;
        }

        ~PagingCommand()
        {
            Dispose();
        }

        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }

        public string ColumnName
        {
            get { return pagingColumnName; }
            set { pagingColumnName = value; }
        }

        public int PageCapacity
        {
            get { return pageCapacity; }
            set { pageCapacity = value; }
        }

        public int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }

        public bool Reverse
        {
            get
            {
                return reverse;
            }
            set
            {
                reverse = value;
            }
        }

        public string Condition
        {
            get { return condition; }
            set { condition = value; }
        }

        public NoebeParameterCollection Parameters
        {
            get { return command.Parameters; }
        }

        public string GetSQL()
        {
            return cache.GetGenerator().GetPagingSQL(tableName, pagingColumnName, condition, pageCapacity, pageIndex, reverse);
        }

        public void Dispose()
        {
            this.cache = null;
            command.Dispose();
            this.command = null;
        }

        public DataTable ExecuteReader()
        {
            string sql = GetSQL();

            command.SQL = sql;

            return command.ExecuteReader();
        }

    }
}
